package crp.core.portal.domain.repositories;

import com.qwlabs.lang.C2;
import com.qwlabs.lang.S2;
import com.qwlabs.panache.Where;
import crp.core.portal.domain.models.NewsArticleEntity;
import crp.core.portal.domain.models.NewsArticleQuery;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Sort;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class NewsArticleRepository implements PanacheRepositoryBase<NewsArticleEntity, String> {

    public PanacheQuery<NewsArticleEntity> by(NewsArticleQuery query) {
        Where where = Where.create();
        if (C2.isNotEmpty(query.getStatuses())) {
            where.and("status in (:statuses)", "statuses", query.getStatuses());
        }
        if (S2.isNotEmpty(query.getKeyword())) {
            where.and("(title like :keyword or content like :keyword)",
                    "keyword", "%" + query.getKeyword() + "%");
        }
        return find(where.get(), Sort.descending("createdAt"), where.getParameters());
    }
}
